package org.csu.mypetstore_seller.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore_seller.domain.Item;
import org.csu.mypetstore_seller.domain.Order;
import org.csu.mypetstore_seller.domain.OrderStatus;
import org.csu.mypetstore_seller.persistence.OrderMapper;
import org.csu.mypetstore_seller.persistence.OrderStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderStatusMapper orderStatusMapper;

    //通过用户名查找订单
    public List<Order> GetOrderListByUserId(String userId){
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",userId);

        return orderMapper.selectList(queryWrapper);
    }

    public List<Order> GetAllOrder(){
        return orderMapper.selectList(null);
    }

    //通过订单id查找一个订单
    public Order GetOrderByOrderId(int orderId){
        return orderMapper.selectById(orderId);
    }

    //通过订单id查找订单(list列表）
    public List<Order> GetOrderListByOrderId(int orderId){
        Order order=GetOrderByOrderId(orderId);
        List<Order> list=new ArrayList<>();
        list.add(order);
        return list;
    }

    //通过城市查找订单
    public List<Order> GetOrderListByCity(String city){
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("shipcity",city);
        return orderMapper.selectList(queryWrapper);
    }

    //通过shipcountry查找订单
    public List<Order> GetOrderListByShipCountry(String country){
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("shipcountry",country);
        return orderMapper.selectList(queryWrapper);
    }

    //通过订单id删除订单
    public void DeleteOrderByOrderId(int orderId){
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("orderid", orderId);
        updateWrapper.set("delete_flag", -1);
        orderMapper.update(null, updateWrapper);
        orderStatusMapper.deleteById(orderId);
    }

    //更新订单
    public void UpdateOrder(Order order,String id){
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        System.out.println(id);
        // 设置更新条件：更新 orderid 为 id
        updateWrapper.eq("orderid", id);
        orderMapper.update(order,updateWrapper);
    }



    //查找订单合集
    public List<Order> GetOrder(String method,String str){
        if(method.equals("userid"))
            return GetOrderListByUserId(str);
        else if(method.equals("orderid"))
                return GetOrderListByOrderId(Integer.parseInt(str));
                else if(method.equals("shipcity"))
                    return GetOrderListByCity(str);
                        else if(method.equals("shipcity"))
                        return GetOrderListByShipCountry(str);
                        else return GetAllOrder();
    }

    public Integer getNum(){
        System.out.println(orderMapper.selectList(null));

        return orderMapper.selectList(null).size();
    }

}
